package com.niit.mall.api.config;


import com.niit.mall.api.fallback.*;
import com.niit.mall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.api.config
 * @Author: ChuXuan
 * @CreateTime: 2025-06-04  16:22
 * @Description: Feign配置类
 * @Version: 1.0
 */

public class DefaultFeignConfig {
	
	@Bean
	public Logger.Level feignLoggerLevel() {
		// 全部输出
//		return Logger.Level.FULL;
		return Logger.Level.NONE;
	}
	@Bean
	public RequestInterceptor userInfoRequestInterceptor(){
		return new RequestInterceptor() {
			@Override
			public void apply(RequestTemplate template) {
				// 获取登录用户
				Long userId = UserContext.getUser();
				if(userId == null) {
					// 如果为空则直接跳过
					return;
				}
				// 如果不为空则放入请求头中，传递给下游微服务
				template.header("user-info", userId.toString());
			}
		};
	}
	
	@Bean
	public AddressClientFallbackFactory addressClientFallbackFactory(){
		return new AddressClientFallbackFactory();
	}
	@Bean
	public CartClientFallbackFactory cartClientFallbackFactory(){
		return new CartClientFallbackFactory();
	}
	@Bean
	 public OrderClientFallbackFactory orderClientFallbackFactory(){
		return new OrderClientFallbackFactory();
	}
	@Bean
	public PayClientFallbackFactory payClientFallbackFactory(){
		return new PayClientFallbackFactory();
	}
	@Bean
	 public ProductClientFallbackFactory productClientFallbackFactory(){
		return new ProductClientFallbackFactory();
	}
	@Bean
	public UserClientFallbackFactory userClientFallbackFactory(){
		return new UserClientFallbackFactory();
	}
}
